package com.youlu.campus.admin.data.controller;


import com.youlu.campus.admin.utils.ExcelUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.BuildDataExcelVO;
import com.youlu.campus.service.data.BuildDataExcelService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class ExportBuildDataExcelController {

    @Autowired
    private BuildDataExcelService buildDataExcelService;


    @PostMapping("/public/build-data-excel/import")
    public RestResult buildDataExcel(@RequestParam("file") MultipartFile file){
        List<BuildDataExcelVO> excelDataList = ExcelUtils.readExcel(file, BuildDataExcelVO.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return RestResultBuilder.builder().failure("导入文件错误").build();
        }
        buildDataExcelService.buildDataExcel(excelDataList);
        return RestResultBuilder.builder().success(true).build();
    }


    @PostMapping("/public/init/user-order/address")
    public RestResult initUserOrderAddress(@RequestParam("file") MultipartFile file){
        List<BuildDataExcelVO> excelDataList = ExcelUtils.readExcel(file, BuildDataExcelVO.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return RestResultBuilder.builder().failure("导入文件错误").build();
        }
        buildDataExcelService.initUserOrderAddress(excelDataList);
        return RestResultBuilder.builder().success(true).build();
    }


}
